package java44th.group.utils;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.PutObjectResult;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

/**
 * @Aurhor: 徐云飞
 * @DATE: 2022/10/15 20:33
 * @Description:
 * @Version 1.0
 */
public class Upload {

    public  String Upload(MultipartFile myfile) throws IOException {

        String accessKeyId = "LTAI5tMbnzwQW1eT8VQyMXM4";
        String accessKeySecret = "lEPvt0WdDtF88YllKoqQ3GYQNa4CLJ";
        String endPoint = "oss-cn-hangzhou.aliyuncs.com";
        String bucket = "xyf12345";
        InputStream inputStream = myfile.getInputStream();
        //准备个文件
        String fileName = UUID.randomUUID().toString().replaceAll("-", "") + ".jpg";
        System.out.println(fileName); //上传完成 要通过bucket+endpoint+文件名进行访问
        OSSClient ossClient = new OSSClient(endPoint, accessKeyId, accessKeySecret);

        PutObjectResult putObjectResult = ossClient.putObject(bucket, fileName, inputStream);
        ObjectMapper objectMapper = new ObjectMapper();
        String s = objectMapper.writeValueAsString(putObjectResult);

        String url = "http://" + bucket + "." + endPoint + "/" + fileName;
        return url;


    }
}
